﻿using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Web.SessionState;
using System.Text;

namespace IFD2
{
    public class same_flt_prod_sale_advance : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //if (context.Request.QueryString["input"] == null)
            //{
            //    context.Response.Write("?:Lost paramater!");
            //    context.ApplicationInstance.CompleteRequest();
            //    return;
            //}
            if (context.Session["ACL"].ToString().IndexOf("IA1") < 0)
            {
                context.Response.Write("?:Unauthorized!");
                context.ApplicationInstance.CompleteRequest();
                return;
            }
            if (context.Request.QueryString["flt_date"] == null)
            {
                context.Response.Write("?:Lost parameters!");
                context.ApplicationInstance.CompleteRequest();
                return;
            }
            SqlCommand cmd = new SqlCommand();
            SqlParameter para;
            StringBuilder buf = new StringBuilder();
            DataTable dt_ret = new DataTable();
            int i;
            try
            {
                cmd.CommandText = "bll_flt_get_product_sale_on_day";
                para = cmd.Parameters.AddWithValue("@airline", context.Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@flt_no", context.Request.QueryString["flt_no"]);
                para = cmd.Parameters.AddWithValue("@date_focus", context.Request.QueryString["flt_date"]);
                para = cmd.Parameters.AddWithValue("@week_on_week", context.Request.QueryString["weekonweek"]);
                para = cmd.Parameters.AddWithValue("@span", context.Request.QueryString["span"]);
                i = TinyDAL.Exec(ref cmd, ref dt_ret);
            }
            catch (Exception ex)
            {
                if (context.Application["DEBUG"].ToString() == "1")
                {
                    context.Response.Write(ex.Message);
                    context.ApplicationInstance.CompleteRequest();
                }
                else
                {
                    context.Response.Write("Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.");
                    context.ApplicationInstance.CompleteRequest();
                }
                return;
            }

            if (i == 0)
            {
                //continue
            }
            else if (i == 1)
            {
                context.Response.Write("?:Too large span!");
                context.ApplicationInstance.CompleteRequest();
                return;
            }
            else
            {
                context.Response.Write("?:Error=" + i.ToString());
                context.ApplicationInstance.CompleteRequest();
                return;
            }

            if (dt_ret.Rows.Count == 0)
            {
                context.Response.Write("No Data!");
                context.ApplicationInstance.CompleteRequest();
                return;
            }
            int day_span = Convert.ToInt16(context.Request.QueryString["span"]), day_diff, last_day_diff = -999;
            bool is_weekonweek, is_firstday = true;
            if (context.Request.QueryString["weekonweek"] == "1")
                is_weekonweek = true;
            else
                is_weekonweek = false;
            DateTime fltdate1, cur_date;
            DateTime last_flt_date = Convert.ToDateTime("1950-1-1");
            int last_advance = 999999;
            TimeSpan ts;
            buf.Append("<div class='table-responsive'><table border='0' cellpadding= '0' cellspacing='0' style='word-break:keep-all;white-space:nowrap;'><tr align='center'><td></td>");
            i = day_span * -1;
            fltdate1 = Convert.ToDateTime(context.Request.QueryString["flt_date"]);
            do
            {
                if (!is_weekonweek)
                    cur_date = fltdate1.AddDays(i);
                else
                    cur_date = fltdate1.AddDays(i * 7);
                //buf.Append("<td style='width:150px;BORDER-LEFT:#aaaaaa 1px solid;'><b>" + cur_date.ToShortDateString().Substring(5) + "</b></td>");// style='width:140px;'
                buf.Append("<td style='BORDER-LEFT:#aaaaaa 1px solid;'><b>" + cur_date.GetDateTimeFormats('M')[0].ToString() + "</b></td>");// style='width:140px;'
                i++;
            } while (i <= day_span);
            buf.Append("</tr><tr>");

            foreach (DataRow row in dt_ret.Rows)
            {
                ts = Convert.ToDateTime(row["flt_date"]).Subtract(fltdate1);
                if (!is_weekonweek)
                    day_diff = ts.Days + day_span;
                else
                    day_diff = ts.Days / 7 + day_span;
                if (last_advance != Convert.ToInt32(row["advance"]))
                {
                    if (last_advance != 999999)
                        buf.Append("</td></tr>");
                    buf.Append("<tr><td>" + row["advance"].ToString() + "</td>");
                    //for (i = 1; i <= day_diff; i++)
                    //    buf.Append("<td></td>");
                    last_advance = Convert.ToInt32(row["advance"]);
                    last_day_diff = day_diff;
                    last_flt_date = Convert.ToDateTime(row["flt_date"]);
                    is_firstday = true;
                }
                if (last_flt_date != Convert.ToDateTime(row["flt_date"]) && !is_firstday)
                {
                    buf.Append("</td><td>");
                }
                if (!is_firstday)
                {
                    for (i = 1; i < day_diff - last_day_diff; i++)
                        buf.Append("<td></td>");
                    last_day_diff = day_diff;
                }
                else
                {
                    for (i = 0; i < day_diff; i++)
                        buf.Append("<td></td>");
                    is_firstday = false;
                }

                //if (last_flt_date != Convert.ToDateTime(row["flt_date"]))
                //{
                //    if (!is_firstday)
                //        buf.Append("</td>");
                //    buf.Append("<td>");
                //    last_flt_date = Convert.ToDateTime(row["flt_date"]);
                //    last_day_diff = day_diff;
                //}
                buf.Append("<td style='font-family:courier new;'>" + row["ori_city"].ToString().TrimEnd() + "-" + row["des_city"].ToString().TrimEnd()
                    + " " + row["cabin"].ToString() + ":" + (row["class"].ToString().TrimEnd() + "   ").Substring(0, 3) + row["fare_code"].ToString().TrimEnd()
                    + ":" + row["seats"].ToString() + "&nbsp;&nbsp;<br/>");
                is_firstday = false;
            }
            buf.Append("</td></tr><tr><td>&nbsp;</td></tr><tr><td colspan='20'>Legend: segment:cabin:class:product:seats</td></tr></table></div>");
            context.Response.Write(buf.ToString());
            context.ApplicationInstance.CompleteRequest();
            return;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}